home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-02 | 5.2 KB | 228 lines | [TEXT/KAHL] |
- /****
- * CLaughsDoc.cp
- *
- * Implementation of a document class full of laughs.
- *
- * Copyright © 1994 NeoLogic Systems. All rights reserved.
- *
- * This sample app is derived from a contribution originally made
- * by Paul Gee (Gee, thanks Paul!).
- *
- ****/
-
- #include "NeoTypes.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include CNeoDatabaseNativeH
- #include CNeoIndexIteratorH
- #include CNeoSelectH
- #include "CLaughsApp.h"
- #include "CLaughsDoc.h"
- #include "CPerson.h"
-
- /***
- * CLaughsDoc
- *
- * This is the document's initialization method.
- *
- ***/
- CLaughsDoc::CLaughsDoc(CApplication *aSupervisor, const Boolean aPrintable, const Boolean aNewDatabase)
- : CNeoDocRoot(kLaughsSig, kLaughsFileType, aPrintable, aNewDatabase, TRUE, FALSE)
- {
- // A document that opens a window would not need to activate itself explicitly.
- Activate();
- }
-
- /***
- * buildWindow
- *
- * This is the auxiliary window-building method that the
- * NewFile and OpenFile methods use to create a window.
- *
- * Laughs doesn't need to explicitly create a window,
- * printf will do that for us.
- *
- ***/
- void CLaughsDoc::buildWindow(void)
- {
- // Nothing to do.
- }
-
- /***
- * NewFile
- *
- * When the user chooses New from the File menu, the createDocument
- * method in your application will call the newly created document's
- * NewFile message.
- *
- * This method should begin by calling inherited. After that it might
- * want to title the window and set the default name which will appear
- * in the standard get file dialog. Laughs calls DoSaveFileAs just
- * so the user doesn't have to choose the Save menu item. Your
- * application may decide otherwise.
- *
- ***/
- void CLaughsDoc::NewFile(void)
- {
- CNeoDatabase * database = getDatabase();
-
- inherited::NewFile();
-
- // Set the default name which appears in the get file dialog.
- database->Specify("\pLaughter", 0);
-
- // Stuff the database with objects.
- createObjects();
-
- // Commit the changes that we made to the database.
- DoSaveFileAs();
-
- printf("\nDone!\n\n");
- }
-
- /***
- * OpenFile
- *
- * When the user chooses Open… from the File menu, the OpenDocument
- * method in your application class will let the user choose a file
- * and then send a newly created document this message. The information
- * about the file is in the SFReply record.
- *
- ***/
- void CLaughsDoc::OpenFile(SFReply *macSFReply)
- {
- fOpenMode = fsRdPerm;
- inherited::OpenFile(macSFReply);
-
- // Find each of the objects in the database in turn.
- printOut();
-
- // That's all folks!
- printf("\nDone!\n\n");
- }
-
- /***
- * createObjects
- *
- * Add three people to the database, two jokers and a clown.
- *
- ***/
- void CLaughsDoc::createObjects(void)
- {
- CJoke * joke1;
- CJoke * joke2;
- CJoker * joker;
- CClown * clown;
- CNeoDatabase * database = getDatabase();
- CNeoString string;
- char name[64];
-
- // Tell them what we're about to do.
- database->getName(string);
- BlockMove(&string[1], name, string[0]);
- name[string[0]] = 0;
- printf("Storing 2 Jokers & a Clown in \"%s\".\n", name);
-
- // Know any good jokes? How 'bout this one...
- joke1 = new CJoke("The world’s shortest poem: Flees. Adam had'em.");
-
- // Add it to the database.
- // Note: An object ID is assigned automaticly by addObject.
- database->addObject(joke1);
-
- // Is this a joke???
- joke2 = new CJoke("My dogs got no nose?");
-
- // Add it to the database.
- database->addObject(joke2);
-
- // Create a joker object.
- joker = new CJoker("\pJack");
-
- // Teach it a couple of jokes.
- joker->learnJoke(joke1);
- joker->learnJoke(joke2);
-
- // Add it to the database.
- database->addObject(joker);
-
- // Don't need this guy any more. Remove our reference to it.
- joker->unrefer();
- joker = nil;
-
- // Create a clown.
- clown = new CClown("\pFred");
-
- // Add it to the database.
- database->addObject(clown);
-
- // Build up its arsenal.
- clown->bakePie("Jello");
- clown->bakePie("Marshmellow");
- clown->bakePie("Custard");
- clown->bakePie("Cool Whip®");
- clown->bakePie("Yogurt");
-
- // Remember to remove our reference when we're done.
- clown->unrefer();
- clown = nil;
-
- // Create another joker.
- joker = new CJoker("\pHarry");
-
- // Add it to the database.
- database->addObject(joker);
-
- // This guy steals jokes.
- joker->learnJoke(joke2);
-
- // Remove our reference to the joker and the jokes.
- joker->unrefer();
- joker = nil;
- joke1->unrefer();
- joke1 = nil;
- joke2->unrefer();
- joke2 = nil;
- }
-
- /***
- * printOut
- *
- * Find in turn each of the three objects in the database, two jokers and
- * a clown, then commit the changes to disk.
- *
- ***/
- void CLaughsDoc::printOut(void)
- {
- CPerson * person;
- CNeoNameSelect key("\p");
- CNeoDatabase * database = getDatabase();
- CNeoIndexIterator iterator(database, kPersonID, &key, TRUE, TRUE);
- CNeoString string;
- char name[64];
-
- // Tell them what we're about to do.
- database->getName(string);
- BlockMove(&string[1], name, string[0]);
- name[string[0]] = 0;
- printf("Restoring %ld Jokers & %ld Clowns from \"%s\".\n",
- database->getObjectCount(kJokerID, FALSE),
- database->getObjectCount(kClownID, FALSE), name);
-
- // Let's make rand() a little more variable.
- srand((int)time(nil));
-
- // Get the first person object.
- person = (CPerson *)iterator.currentObject();
-
- while(person) {
- person->printName(); // Introductions.
- person->skill(); // Entertain the crowd.
- printf("\n");
- person = (CPerson *)iterator.nextObject(); // Next.
- }
- }
-
-
-